package com.nip.irregularvebsdict.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class IrregularVersService extends Service {

	private IrregularVerbsHandle updateHandle = new IrregularVerbsHandle(this);
	private IrregularVerbsHandle updateWord = new IrregularVerbsHandle(this);
	private Intent intent;
	

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onCreate() {
		updateHandle.removeCallbacks(timeUpdate);
		updateHandle.sendEmptyMessage(ServicesUtilities.ACTION_UPDATE);
		updateHandle.postDelayed(timeUpdate, 10000l);
		
		updateWord.removeCallbacks(workUpdate);
		updateWord.sendEmptyMessage(ServicesUtilities.ACTION_UPDATE_WORD);
		updateWord.postDelayed(workUpdate, 300000l);
		super.onCreate();
	}

	@Override
	public void onStart(Intent intent, int startId) {

	}

	@Override
	public void onDestroy() {
		intent = new Intent(this, IrregularVersService.class);
		startService(intent);
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	private Runnable timeUpdate = new Runnable() {
		
		@Override
		public void run() {
			updateHandle.sendEmptyMessage(ServicesUtilities.ACTION_UPDATE);
			updateHandle.postDelayed(timeUpdate, 20000l);
		}
	};
	
	private Runnable workUpdate = new Runnable() {
		
		@Override
		public void run() {
			updateHandle.sendEmptyMessage(ServicesUtilities.ACTION_UPDATE_WORD);
			updateHandle.postDelayed(workUpdate, 300000l);
			
		}
	};

}
